<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class App_sample_reference extends MY_Controller {
	
	function __construct()
	{
		parent::__construct(); 
		
		// Uses Model
		$this->load->model('sample_reference_model', 'Ref');
		
		// Secure Page With Session
		$this->secure_session();
	}
	
	function index(){
		// Display Index
		$data['title'] = $this->session_menu_label;
		
		viewByTemplate('app_sample_reference/index', $data, 'grid');
	}
	
	function getdata()
	{
		// Ajax Response For Datagrid
		echo $this->Ref->GetJSON();
	}
	
	function create()
	{
		// Display Form Create
		$data['title'] = "Tambah Data Reference";
		$data['data'] = NULL;
		
		$data['mode'] = "create";
		viewByTemplate('app_sample_reference/form', $data, 'form');
	}

	function edit()
	{
		// Display Form Edit
		$data['title'] = "Ubah Data Reference";
		$data['data'] = $this->Master->GetSingleView(get('pk_master'));
		
		$data['mode'] = "edit";
		viewByTemplate('app_sample_reference/form', $data, 'form');
	}
	
	private function validation()
	{
		$error = new ErrorList();

		// Get Error Template
		$tpl_empty		= $this->MessageTemplate("ERR001");
		$tpl_duplicate	= $this->MessageTemplate("ERR010");


		// Validation
		if (post("ref_name") == '') {
			$error->Add("ref_name", "Reference Name", $tpl_empty);
		} else if (post("mode") == "create" && intval($this->get_data("SELECT COUNT(1) FROM sample_reference WHERE UPPER(ref_name) = '".@strtoupper(@trim(post("ref_name")))."'")) > 0) {
			$error->Add("ref_name", "Reference Name", $tpl_duplicate);
		}
		
		return $error;
	}
	
	function ajax_post()
	{
		// Ajax Response For Submit Form
		$is_error = 0;
		$ls_response = "";
		$ls_genid = "";
		
		// Get Error Template
		$tpl_insert_success = $this->MessageTemplate("ERR003");
		$tpl_update_success = $this->MessageTemplate("ERR004");
		
		$error = $this->validation();
		try
		{
			if (count( $error->Get() ) > 0) { $is_error = 1; }
			else
			{
				// Fill Post Data Array
				$data	= post_by_key(array('ref_key','ref_name'));
				
				if (post("mode") == "create")
				{
					$data['create_date']	= NOW;
					$data['create_id']		= USERID;
					
					$this->Master->Add($data);
					
					$ls_response = $tpl_insert_success['error_message'];
				}
				else if (post("mode") == "edit")
				{
					$ref_key	= $data['ref_key'];
					unset($data['ref_key']);
					
					$data['modify_date']	= NOW;
					$data['modify_id']		= USERID;
					
					$this->Master->Update(	$data, 
											array('ref_key'	=> $ref_key));

					$ls_response = $tpl_update_success['error_message'];
				}
			}
		} catch(Exception $e) {
			$is_error = 1; $ls_response = $e->getMessage();
		}
		
		echo $this->PostResponse(json_encode(array(	'is_error'	=> $is_error,
													'fields'	=> $error->GetDictionary(),
													'msg'		=> $ls_response,
													'genid'		=> $ls_genid,
													'form_sender'=> post("form_sender"))
								));
	}

	function ajax_delete()
	{
		$ls_result	= "";
		$ref_key	= get('ref_key');
		
		if ($pk_master != "")
		{
			// Delete Single
			$arr_rule	= array('ref_key' => $ref_key);
			$ls_result = $this->Master->DeleteSingle($arr_rule);
		}
		else
		{
			$num_row = intval($this->data['num_row']['deleted']);
			
			for ($i = 0; $i < $num_row; $i++)
			{
				$row	= $this->data['deleted'][$i];
				
				$arr_rule	= array('ref_key' => $row['ref_key']);
				$ls_result .= $this->Master->DeleteSingle($arr_rule);
			}
		}
		
		echo $ls_result;
	}
}
?>